<template>
  <div class="page-city">
    <mt-header title="当前城市">
        <i slot="left" class="iconfont icon-close" @click="goBack"></i>
    </mt-header>
    <!-- <mt-search v-model="value" cancel-text="取消"></mt-search> -->
    <div class="serch">
      <div class="input-wor" @click="fn5" :style="active">
        <i class="iconfont iconsearch"></i>
        <input type="text" placeholder="请输出城市名或拼音" class="input-text" v-model="value">
        <div class="clean-hear">
          <i class="iconfont icontubiao-quanx" @click="fn4"></i>
        </div>
        <div class="input-btn" @click="fn3" :style="btnsty">取消</div>
      </div>
    </div>
    <!-- 城市列表 -->
    <div class="main" v-show="!value && isshow">
      <div class="left" ref="left">
        <div class="citylist">
          <p class="py">热门城市</p>
          <ul>
            <li v-for="city in hotCityList" :key="city.cityId" @click="fn2(city.name)">{{ city.name }}</li>
          </ul>
        </div>
        <div class="citylist" v-for="item in cityList" :key="item.py" :ref="`list-${item.py}`">
          <p class="py">{{ item.py }}</p>
          <ul>
            <li v-for="city in item.list" :key="city.cityId" @click="fn2(city.name)">{{ city.name }}</li>
          </ul>
        </div>
      </div>
      <div class="right">
        <ul>
          <li v-for="py in pys" :key="py" @click="fn1(py)">{{ py }}</li>
        </ul>
      </div>
    </div>
    <!-- 搜索结果 -->
    <div class="main" v-show="value || !isshow">
      <div class="left">
        <div class="citylist" v-show="searchList.length">
          <ul>
            <li v-for="city in searchList" :key="city.cityId" @click="fn2(city.name)">{{ city.name }}</li>
          </ul>
        </div>
        <div class="citytype" v-show="!searchList.length">
          <img src="" alt="">
          <p>没有找到你想要的城市</p>
      </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCityList } from '../api/city'
export default {
  name: 'City',
  data () {
    return {
      cities: [],
      value: '',
      isshow: true,
      btnsty: 'display:none',
      active: 'width:100%'
      // isShowSearchPanel: false //是否显示搜索结果
    }
  },
  watch: {
    value () {
      this.isshow = false
    }
  },
  methods: {
    goBack () {
      this.$router.back()
    },
    fn1 (py) {
      let top = this.$refs[`list-${py}`][0].offsetTop
      this.$refs['left'].scrollTop = top
    },
    fn2 (name) {
      window.localStorage.setItem('cityname', name)
      this.$router.back()
    },
    fn3 () {
      this.value = ''
      this.isshow = true
    },
    fn4 () {
      this.isshow = false
      this.value = ' '
    },
    fn5 () {
      this.btnsty = 'display:block'
      this.active = 'width:calc(100% - 43px)'
    }
  },
  created () {
    getCityList().then(response => {
      let res = response.data
      this.cities = res.data.cities
    })
  },
  computed: {
    cityList () {
      let result = []
      this.cities.forEach(item => {
        let py = item.pinyin.charAt(0).toUpperCase()
        let index = result.findIndex(city => city.py === py)
        if (index > -1) {
          result[index].list.push(item)
        } else {
          let obj = {
            py,
            list: [item]
          }
          result.push(obj)
        }
      })
      return result.sort((a, b) => a.py.charCodeAt() - b.py.charCodeAt())
    },
    hotCityList () {
      return this.cities.filter(item => item.isHot)
    },
    pys () {
      return this.cityList.map(item => item.py)
    },
    searchList () {
      return this.cities.filter(item => {
        if (this.value.trim()) {
          return item.name.indexOf(this.value.trim()) > -1 || item.pinyin.indexOf(this.value.trim()) > -1
        }
      })
    }
  }
}
</script>

<style lang="scss">
.page-city{
  height: 100%;
  display: flex;
  flex-direction: column;
  .serch{
    position: relative;
    height: 52px;
    padding: 9.5px 15px 0 15px;
    background-color: #f4f4f4;
    .input-wor{
      background-color: #fff;
      position: relative;
      display: inline-block;
      border-radius: 3px;
      height: 30px;
      .iconsearch{
        position: absolute;
        left: 7px;
        top: 0;
        line-height: 30px;
        font-size: 11px;
        color: #ccc
      }
      .input-text{
        position: absolute;
        left: 33.5px;
        top: 0;
        height: 30px;
        width: calc(100% - 65px);
        border: 0;
        margin: 0;
        padding: 0;
        color: #2c3e50;
        outline:none;
      }
      .clean-hear{
        position: absolute;
        right: 7px;
        top: 0;
        line-height: 30px;
        font-size: 21px;
        .icontubiao-quanx{
          color: #bdc0c5;

        }
      }
      .input-btn{
        width: 43px;
        position: absolute;
        display: inline-block;
        text-align: right;
        font-size: 14px;
        line-height: 30px;
        right: -46px;
      }
    }
  }
  .mint-header {
    background-color: #fff;
  }
  .mint-header-title {
    color: #000;
    font-size: 18px;
  }
  .iconfont {
    font-size: 18px;
    color: #000;
  }
  .mint-search {
    height: auto;
  }
  .main {
    flex: 1;
    display: flex;
    overflow: hidden;

    .left {
      position: relative;
      flex: 1;
      overflow-y: auto;
    }
    .right {
      display: flex;
      justify-content: center;
      align-items: center;
      width: 18px;
      li {
        width: 18px;
        height: 18px;
        text-align: center;
      }
    }
  }
  .citylist {
    .py {
      height: 36px;
      line-height: 36px;
      background: #f4f4f4;;
    }
    li {
      height: 40px;
      line-height: 40px;
      border-bottom: 1px solid #ededed;
    }
  }
  .citytype{
    height: 180px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    img{
      width: 90px;
    }
    p{
      color:#ccc
    }
  }
  .mint-searchbar{
    background: #f4f4f4
  }
  .mint-searchbar-cancel{
    color: #2c3e50;
  }
}
</style>
